package com.projles.ttd.util;

import java.util.HashMap;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Vibrator;

import com.projles.ttd.OpcoesActivity;
import com.projles.ttd.R;

public class Player {
	
	private Context context;

	public static final int EXCELLENT = 1;
	public static final int DOH = 2;
	public static final int PALMAS = 3;
	public static final int CLICK = 4;
	public static final int AVISO_AZAR = 5;
	
	private float volume;
	private boolean vibra = true;
	
	private SoundPool soundPool;
	private HashMap<Integer, Integer> soundPoolMap;
	
	private SharedPreferences config;
	Vibrator vibrator;
	private static Player player;
	
	public static Player getPlayer(Context context) {
		if (player == null) {
			player = new Player(context);
		}
		player.setContext(context);
		return player;
	}

	private Player(Context context) {
		config = context.getSharedPreferences(OpcoesActivity.PREFS_NAME, 0);
		vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
		setContext(context);
		initSounds();
		volume = getVolume();
		setMute(config.getBoolean(OpcoesActivity.MUTE, false));
		setVibra(config.getBoolean(OpcoesActivity.VIBRA, true));
	}
	
	private void setContext(Context context) {
		this.context = context;
	}
	
	private Context getContext() {
		return this.context;
	}
	
	public void setMute(boolean mute) {
		if (mute) {
			volume = 0;
		} else {
		    volume = getVolume();
		}
	}
	
	public boolean isMuted() {
		return volume == 0;
	}
	
	public void setVibra(boolean vibra) {
		this.vibra = vibra;
	}
	
	public boolean isVibra() {
		return vibra;
	}
	
	private float getVolume() {
		AudioManager mgr = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
	    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
	    float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);    
	    return streamVolumeCurrent / streamVolumeMax;
	}

	private void initSounds() {
	     soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
	     soundPoolMap = new HashMap<Integer, Integer>();
	     soundPoolMap.put(EXCELLENT, soundPool.load(getContext(), R.raw.acerto, 1));
	     soundPoolMap.put(DOH, soundPool.load(getContext(), R.raw.doh, 1));
	     soundPoolMap.put(PALMAS, soundPool.load(getContext(), R.raw.aplausos, 1));
	     soundPoolMap.put(CLICK, soundPool.load(getContext(), R.raw.clique, 1));
	     soundPoolMap.put(AVISO_AZAR, soundPool.load(getContext(), R.raw.dramatic, 1));
	     
	}
	
	
	public void playSound(int sound, int prio) {
	    soundPool.play(soundPoolMap.get(sound), volume, volume, prio, 0, 1f);     
	}
	
	public void playSound(int sound) {
		playSound(sound, 1);    
	}
	
	public void vibra(int tempo) {
		if (vibra) {
			vibrator.vibrate(tempo);
		}
	}

	public void excellent() {
		vibra(300);
	    playSound(EXCELLENT);
	}
	
	
	public void azar() {
		vibra(300);
	    playSound(AVISO_AZAR);
	}
	
	public void doh() {
		vibra(300);
	    playSound(DOH);
	}
	
	public void aplausos() {
		vibra(500);
	    playSound(PALMAS);
	} 
	
	public void click() {
	    playSound(CLICK, 100);
	} 

}
